package com.yh.csx.crm.webapi.controller;

import com.yh.csx.crm.appapi.controller.BaseController;
import com.yh.csx.crm.core.entity.CommonResponse;
import com.yh.csx.crm.core.model.PageList;
import com.yh.csx.crm.core.util.BeanUtils;
import com.yh.csx.crm.po.SysOrganization;
import com.yh.csx.crm.qo.OrganizationQo;
import com.yh.csx.crm.service.OrganizationService;
import com.yh.csx.crm.service.request.OrganizationReq;
import com.yh.csx.crm.service.request.OrganizationSearchReq;
import com.yh.csx.crm.service.response.OrganizationResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Huang Zhaoping
 * @date 2019/4/11
 */
@Api("部门服务")
@RestController
@RequestMapping("/org")
public class OrganizationController extends BaseController {

    @Autowired
    private OrganizationService organizationService;

    @ApiOperation("获取部门树形列表")
    @GetMapping("/tree")
    public CommonResponse<List<OrganizationResp>> findOrgTree() {
        return visit(() -> {
            List<SysOrganization> list = organizationService.findOrganizationList(new OrganizationQo());
            List<OrganizationResp> rootList = new ArrayList<>();
            Map<Integer, OrganizationResp> nodeMap = list.stream().collect(Collectors.toMap(o -> o.getId(), o -> BeanUtils.convert(OrganizationResp.class, o)));
            list.forEach(org -> {
                OrganizationResp resp = nodeMap.get(org.getId());
                if (org.getParentId() == 0) {
                    rootList.add(resp);
                } else {
                    OrganizationResp parent = nodeMap.get(org.getParentId());
                    if (parent != null) {
                        List<OrganizationResp> childList = parent.getChildList();
                        if (childList == null) {
                            childList = new ArrayList<>();
                            parent.setChildList(childList);
                        }
                        childList.add(resp);
                    }
                }
            });
            return rootList;
        });
    }

    @ApiOperation("查找部门列表")
    @GetMapping("/list")
    public CommonResponse<PageList<SysOrganization>> findOrgList(OrganizationSearchReq req) {
        return visit(() -> {
            OrganizationQo qo = BeanUtils.convert(OrganizationQo.class, req);
            List<SysOrganization> list = organizationService.findOrganizationList(qo);
            return PageList.of(list);
        });
    }

    @ApiOperation("创建部门")
    @PostMapping("/create")
    public CommonResponse<Void> createOrg(@RequestBody OrganizationReq req) {
        return visit(() -> {
            SysOrganization org = BeanUtils.convert(SysOrganization.class, req);
            org.setCreateBy(getUserName());
            org.setUpdateBy(org.getCreateBy());
            organizationService.createOrganization(org);
        });
    }

    @ApiOperation("修改部门信息")
    @PostMapping("/update/{id}")
    public CommonResponse<Void> updateOrg(@PathVariable Integer id, @RequestBody OrganizationReq req) {
        return visit(() -> {
            SysOrganization org = BeanUtils.convert(SysOrganization.class, req);
            org.setId(id);
            org.setUpdateBy(getUserName());
            organizationService.updateOrganization(org);
        });
    }

    @ApiOperation("删除部门")
    @PostMapping("/delete/{id}")
    public CommonResponse<Void> deleteOrg(@PathVariable Integer id) {
        return visit(() -> organizationService.deleteOrganization(id));
    }
}
